package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.RechargeQueryResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 14:30
 */
public class RechargeQueryBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(RechargedBizSlot.class);
    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String channelId = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Channel_Id");
        msgBody.put("channelId", channelId);
        String bankCode = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Bank_Code");
        msgBody.put("bankCode", bankCode);
        String serialType = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Serial_Type");
        msgBody.put("serialType", serialType);
        String paymentSerialNbr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Payment_Serial_Nbr");
        msgBody.put("paymentSerialNbr", paymentSerialNbr);
        String areaCode = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Area_Code");
        msgBody.put("areaCode", areaCode);

        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

      			LOG.info("request body is {}", msgBodyStr.toString());
            LOG.info("webserviceUrl is {}", url);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
            LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        RechargeQueryResponse rechargeQueryResponse = new Gson().fromJson(respStr, RechargeQueryResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", rechargeQueryResponse.getResultCode());
        respDataXml.put("State", rechargeQueryResponse.getRechargeInfo().getState());
        respDataXml.put("Acct_Name",rechargeQueryResponse.getRechargeInfo().getAcctName());
        respDataXml.put("StartTime",rechargeQueryResponse.getRechargeInfo().getStartTime());
        respDataXml.put("Payment_Amount",rechargeQueryResponse.getRechargeInfo().getPaymentAmount());
        respDataXml.put("Acct_ID",rechargeQueryResponse.getRechargeInfo().getAcctId());
        respDataXml.put("Latn_Id",rechargeQueryResponse.getRechargeInfo().getLanId());
        respDataXml.put("Acc_Nbr",rechargeQueryResponse.getRechargeInfo().getAccNbr());
        respDataXml.put("Bill_Serial_Nbr",rechargeQueryResponse.getRechargeInfo().getPaymentSerialNbr());
        String respXmlStr="";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }
}
